package com.longlonggo.util;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求通用类
 * 
 * @author 石马人山 me@longlonggo.com
 * @Created 2018年5月3日 下午3:37:03
 *          </p>
 * @version 100-000-000<br/>
 * @description <br/>
 */
public class RequestUtil {
	
	/**
	 * 获取请求ip
	 * @param request
	 * @return
	 */
	public static String getRequestIP(HttpServletRequest request) {
		String clientIp = request.getHeader("x-forwarded-for");
		if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
			clientIp = request.getHeader("Proxy-Client-IP");
		}
		if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
			clientIp = request.getHeader("WL-Proxy-Client-IP");
		}
		if (clientIp == null || clientIp.length() == 0 || "unknown".equalsIgnoreCase(clientIp)) {
			clientIp = request.getRemoteAddr();
		}
		return clientIp;
	}

	/**
	 * 得到请求的根目录，全路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getBasePath(HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() 
							+ "://" 
							+ request.getServerName() 
							+ (request.getServerPort() == 80 ? "" : ":" + request.getServerPort())
							+ path;
		return basePath;
	}

	/**
	 * 得到结构目录
	 * 
	 * @param request
	 * @return
	 */
	public static String getContextPath(HttpServletRequest request) {
		String path = request.getContextPath();
		return path;
	}
}
